package com.component;

import com.util.GetImage;
import com.util.ScreenUtils;
import com.util.SelectUsers;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ConsumerInterface {
    JFrame jf = new JFrame();

    private String userName;
    final int WIDTH = 850;
    final int HEIGHT = 600;

    //组装视图
    public void init(String userName) throws Exception {
        jf.setTitle("扶贫农产品销售系统："+userName+"，欢迎您");
        this.userName = userName;
        //给窗口设置属性
        jf.setBounds((ScreenUtils.getScreenWidth() - WIDTH) / 2, (ScreenUtils.getScreenHeight() - HEIGHT) / 2, WIDTH, HEIGHT);
        //jf.setResizable(false);
        jf.setIconImage(new GetImage().getImage("logo.png"));

        //设置菜单栏
        JMenuBar jmb = new JMenuBar();
        JMenu jMenu = new JMenu("设置");
        JMenuItem m1 = new JMenuItem("切换账号");
        JMenuItem m2 = new JMenuItem("退出程序");
        m1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new FarmingSystemGUI();
                    jf.dispose();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
        m2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        jMenu.add(m1);
        jMenu.add(m2);
        jmb.add(jMenu);

        jf.setJMenuBar(jmb);

        //设置分割面板
        JSplitPane sp = new JSplitPane();

        //支持连续布局
        sp.setContinuousLayout(true);
        sp.setDividerLocation(150);
        sp.setDividerSize(7);

        //设置左侧内容
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("消费者的系统管理");
        DefaultMutableTreeNode browseProductManage = new DefaultMutableTreeNode("浏览农产品");
        DefaultMutableTreeNode purchaseProductManage = new DefaultMutableTreeNode("选购农产品");
        DefaultMutableTreeNode viewOrderManage = new DefaultMutableTreeNode("查看订单");

        root.add(browseProductManage);
        root.add(purchaseProductManage);
        root.add(viewOrderManage);

        Color color = new Color(203,220,217);
        JTree tree = new JTree(root);
        MyRenderer myRenderer = new MyRenderer();
        myRenderer.setBackgroundNonSelectionColor(color);
        myRenderer.setBackgroundSelectionColor(new Color(140,140,140));
        tree.setCellRenderer(myRenderer);

        //获取用户的编号
        int UserID = new SelectUsers().getUserID(userName);

        tree.setBackground(color);
        //设置当前tree默认选中农产品管理
        tree.setSelectionRow(1);
        tree.addTreeSelectionListener(new TreeSelectionListener() {
            //当条目选中变化后，这个方法会执行
            @Override
            public void valueChanged(TreeSelectionEvent e) {
                //得到当前选中的结点对象
                Object lastPathComponent = e.getNewLeadSelectionPath().getLastPathComponent();

                if (browseProductManage.equals(lastPathComponent)){
                    //sp.setRightComponent(new JLabel("这里进行浏览农产品..."));
                    sp.setRightComponent(new BrowseProductManageComponent(jf));
                    sp.setDividerLocation(150);
                }else if (purchaseProductManage.equals(lastPathComponent)){
                    //sp.setRightComponent(new JLabel("这里进行选购农产品，并生成订单..."));
                    sp.setRightComponent(new PurchaseProductManageComponent(jf,UserID));
                    sp.setDividerLocation(150);
                } else if (viewOrderManage.equals(lastPathComponent)){
                    //sp.setRightComponent(new JLabel("这里进行查看订单信息。..."));
                    sp.setRightComponent(new ViewOrderManageComponent(jf,UserID));
                    sp.setDividerLocation(150);
                }

            }
        });


        sp.setRightComponent(new BrowseProductManageComponent(jf));
        sp.setLeftComponent(tree);
        jf.add(sp);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    //自定义结点绘制器
    private class MyRenderer extends DefaultTreeCellRenderer {
        private ImageIcon rootIcon = null;
        private ImageIcon browseProductManageIcon = null;
        private ImageIcon purchaseProductManageIcon = null;
        private ImageIcon viewOrderManageIcon = null;

        public MyRenderer() {
            rootIcon = new ImageIcon(new GetImage().getImage("systemManage.png"));
            browseProductManageIcon = new ImageIcon(new GetImage().getImage("BrowseProduct.png"));
            purchaseProductManageIcon = new ImageIcon(new GetImage().getImage("PurchaseProduct.png"));
            viewOrderManageIcon = new ImageIcon(new GetImage().getImage("ViewOrder.png"));
        }

        //当绘制树的每个结点时，都会调用这个方法
        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            //使用默认绘制
            super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);

            ImageIcon image = null;
            switch (row) {
                case 0:
                    image = rootIcon;
                    break;
                case 1:
                    image = browseProductManageIcon;
                    break;
                case 2:
                    image = purchaseProductManageIcon;
                    break;
                case 3:
                    image = viewOrderManageIcon;
                    break;
            }

            this.setIcon(image);
            return this;
        }
    }

//    public static void main(String[] args) {
//        try {
//            new ConsumerInterface().init(userName);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

}

